<?php
namespace app\common\model;

class GoodsCart extends Base
{
    /**
     * 购物车列表
     */
    public static function getCartList($user_id, $page = 1, $limit = 10)
    {
        $cartList = GoodsCart::where(['user_id' => $user_id])
            ->field('id, shop_id, goods_id, price, number, sku')
            ->order('create_time DESC')
            ->limit(($page - 1) * $limit, (int)$limit)
            ->select();

        $list = [];
        $shopIds = [];
        foreach($cartList as $v){
            if(in_array($v['shop_id'], $shopIds)){
                $list[array_search($v['shop_id'], $shopIds)]['price'] += $v['price'];
                continue;
            }

            $list[] = [
                'shop_id'    => $v['shop_id'],
                'shop_name'  => Shop::getVal(['id' => $v['shop_id']], 'name'),
                'price'      => $v['price'],
            ];

            $shopIds[] = $v['shop_id'];
        }
        
        foreach($cartList as $info){
            $goodsInfo = Goods::getFind(['id' => $info['goods_id']], 'id, name, small_img');

            if($goodsInfo === false) continue;

            foreach($list as $k => $item){
                $goodsInfo['cart_id'] = $info['id'];
                $goodsInfo['price']   = $info['price'];
                $goodsInfo['number']  = $info['number'];
                $goodsInfo['sku']     = $info['sku'];

                if($item['shop_id'] == $info['shop_id']) $list[$k]['goods_list'][] = $goodsInfo;
            }
        }

        return $list;
    }

    /**
     * 确认订单
     */
    public static function orderConfirm($user_id, $goodsIds)
    {
        $userInfo = User::getFind(['id' => $user_id]);

        $cartList = GoodsCart::where([['user_id', '=', $user_id], ['goods_id', 'in', $goodsIds]])
            ->field('id, shop_id, goods_id, price, number, sku')
            ->order('create_time DESC')
            ->select();

        $list = [];
        $shopIds = [];
        foreach($cartList as $v){
            if(in_array($v['shop_id'], $shopIds)){
                $list[array_search($v['shop_id'], $shopIds)]['price'] += $v['price'];
                continue;
            }

            $list[] = [
                'shop_id'    => $v['shop_id'],
                'shop_name'  => Shop::getVal(['id' => $v['shop_id']], 'name'),
                'price'      => $v['price'],
            ];

            $shopIds[] = $v['shop_id'];
        }
        
        foreach($cartList as $info){
            $goodsInfo = Goods::getFind(['id' => $info['goods_id']]);

            $goodsInfos = [
                'id'           => $goodsInfo['id'],
                'name'         => $goodsInfo['name'],
                'small_img'    => $goodsInfo['small_img'],
                'type'         => $goodsInfo['type'],
            ];

            if($goodsInfo === false) continue;

            foreach($list as $k => $item){
                $goodsInfos['price']  = $info['price'];
                $goodsInfos['number'] = $info['number'];
                $goodsInfos['sku']    = $info['sku'];

                switch($goodsInfo->getData('type')){
                    case 2:
                        $list[$k]['user_brokerage']     = $userInfo['money_brokerage'];

                        if(isset($list[$k]['rebate_brokerage'])){
                            $list[$k]['rebate_brokerage'] += $goodsInfo['price'] * $goodsInfo['brokerage_ratio'];
                        }else{
                            $list[$k]['rebate_brokerage'] = $goodsInfo['price'] * $goodsInfo['brokerage_ratio'];
                        }
                        
                        $goodsInfos['rebate_brokerage'] = $goodsInfo['price'] * $goodsInfo['brokerage_ratio'];
                        break;
                    case 3:
                        $list[$k]['user_money_red']     = $userInfo['money_red'];

                        if(isset($list[$k]['rebate_money_red'])){
                            $list[$k]['rebate_money_red'] += $goodsInfo['money_red'];
                        }else{
                            $list[$k]['rebate_money_red'] = $goodsInfo['money_red'];
                        }
                        
                        $goodsInfos['rebate_money_red'] = $goodsInfo['money_red'];
                        break;
                    case 4:
                        if(isset($list[$k]['rebate_point'])){
                            $list[$k]['rebate_point'] += $goodsInfo['price'] * $goodsInfo['point_ratio'];
                        }else{
                            $list[$k]['rebate_point'] = $goodsInfo['price'] * $goodsInfo['point_ratio'];
                        }
                        
                        $goodsInfos['rebate_point'] = $goodsInfo['price'] * $goodsInfo['point_ratio'];
                        break;
                }

                if($item['shop_id'] == $info['shop_id']) $list[$k]['goods_list'][] = $goodsInfos;
            }
        }

        return $list;
    }
}